$(function () {
    $("#page").load(URI + "/rbac/html/page.html");
});

//查询所有数据
function findAll() {
    //trim()去掉两边的空格
    let data = {
        userId:sessionStorage.getItem("userId"),
        english: $("#english").val(),
        chinese: $("#chinese").val(),
        pageNum: $("#pageNum").val(),
        pageSize: $("#pageSize").val()
    };
    let result = myAjax("/api/dw/word/myCollection", data, "post");
    // console.log(resultList);
    // let  result = adminLogin(resultList);
    // 总条数
    if(result.code ===200){
        $("#totalCount").html(result.data.count);
        setData(result.data.pageInfo.list);
        setLike(result.data.pageInfo.list,result.data.likeList);
    }else{
        alert(result.message);
    }

}

//遍历查询到的数据
function setData(data) {
    let html = "";
    for (let i = 0; i < data.length; i++) {
        html += "<div class=\"wordInfo\">\n" +
            "        <div class=\"english\">\n" +
            "             " + clearNull(data[i].english) + "\n" +
            "        </div>\n" +
            "        <div class=\"chinese\">\n" +
            "           " + clearNull(data[i].chinese) + "\n" +
            "        </div>\n" +
            "        <div class=\"createTime\">\n" +
            "            <div class=\"createTime\">" + clearNull(data[i].createTime) + "</div>\n" +
            "            <div class=\"icon\">\n" +
            "                <div>\n" +
            "                    <div><i id=\"collect\" class=\"layui-icon layui-icon-rate-solid\" style='color: #1E9FFF;' title=\"收藏\" onclick=\"delCollect(" + data[i].id + ")\"></i></div>\n" +
            "                </div>\n" +
            "                <div>\n" +
            "                    <div><i id='like" + data[i].id + "' class=\"layui-icon layui-icon-praise\" style='' title=\"点赞\" onclick=\"giveALike(" + data[i].id + ")\"></i></div>\n" +
            "                    <div id='likeNum'>" + stringToInt(data[i].likeNum) + "</div>\n" +
            "                </div>\n" +
            "            </div>\n" +
            "        </div>\n" +
            "    </div>";
    }
    $("#tbody").html(html);
}

/***
 * 清除数据中的undefined和null将其置换为空，不显示
 */

function clearNull(value) {
    return value === null || value === undefined ? "" : value;
}

function stringToInt(value) {
    return value === null || value === undefined ? 0 : value;
}


/**
 * 点赞
 */
function giveALike(id) {
    //先判断图标样式
    let rate = $("#like" + id).attr("style");
    if (rate === "") {
        let data = {
            //获取当前登录用户id
            // let userId = sessionStorage.getItem("userId");
            //获取当前单词的id
            // let wordId = id;
            userId: sessionStorage.getItem("userId"),
            wordId: id
        };
        let result = myAjax("/api/dw/word/like", data, "post");
        if (result.data === 1) {
            // 点赞成功，更换图标样式
            // alert(result.message);
            $("#like" + id).attr("style", "color: #1E9FFF;");
            //刷新父页面
            x_admin_father_reload();
        } else {
            alert(result.message);
        }
    } else {
        delLike(id);
    }

}

/**
 * 取消点赞
 */
function delLike(id) {
    let data = {
        //获取当前登录用户id
        // let userId = sessionStorage.getItem("userId");
        //获取当前单词的id
        // let wordId = id;
        userId: sessionStorage.getItem("userId"),
        wordId: id
    };
    let result = myAjax("/api/dw/word/delLike", data, "post");
    if (result.data === 1) {
        //取消收藏成功后，将图标换为空心
      // alert(result.message);
        $("#like" + id).attr("style", "");
        //刷新父页面
        x_admin_father_reload();
    } else {
        alert(result.message);
    }
}



/**
 * 取消收藏
 */
function delCollect(id){
    let data = {
        //获取当前登录用户id
        // let userId = sessionStorage.getItem("userId");
        //获取当前单词的id
        // let wordId = id;
        userId:sessionStorage.getItem("userId"),
        wordId:id
    };
    let result = myAjax("/admin/word/delCollect",data,"post");
    if(result.data === 1) {
        //取消收藏成功后，将图标换为空心五角星
        // alert(result.message);
        // //先移除空心五角星class样式
        // $("#collect").removeClass("layui-icon-rate-solid");
        // //再增加实心五角星
        // $("#collect").addClass("layui-icon-rate");
        $("#collect").attr("class", "layui-icon layui-icon-rate");
        $("#collect").attr("style", "");
        $("#collect").attr("title", "点击收藏");
        //刷新父页面
        x_admin_father_reload();
    }
}
/**
 * 点赞图标修改
 */
function setLike(data, value) {
    for (let i = 0; i < data.length; i++) {
        for (let j = 0; j < value.length; j++) {
            $("#like" + value[j].id + "").attr("style", "color: #1E9FFF;");
        }
    }
}






